package com.ayq.activiti.manager.domain.custom.repository;

import com.ayq.activiti.manager.domain.custom.entity.DisDeployment;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>activiti自定义查询</p>
 *
 * @Author ayq
 * @Date 2021/3/26 9:29
 */
@Mapper
public interface CustomActivitiMapper extends BaseMapper<DisDeployment> {

    @Select("SELECT\n" +
            "  t3.`NAME_` AS `d_name`,\n" +
            "  t3.`KEY_` AS `d_key`,\n" +
            "  t3.`DEPLOY_TIME_` AS `deployment_time`,\n" +
            "  t4.`ID_`," +
            "  t3.`ENGINE_VERSION_`,\n" +
            "  t4.`NAME_` AS `p_name`,\n" +
            "  t4.`KEY_` AS `p_key`,\n" +
            "  t4.`VERSION_`,\n" +
            "  t4.`DEPLOYMENT_ID_`,\n" +
            "  t4.`RESOURCE_NAME_`,\n" +
            "  t4.`DGRM_RESOURCE_NAME_`  AS `diagram_resource_name`,\n" +
            "  t4.`SUSPENSION_STATE_`\n" +
            "FROM\n" +
            "  `ACT_RE_DEPLOYMENT` t3\n" +
            "  INNER JOIN\n" +
            "    (SELECT\n" +
            "      *\n" +
            "    FROM\n" +
            "      `ACT_RE_PROCDEF` t2\n" +
            "    WHERE t2.`ID_` IN (\n" +
            "        (SELECT\n" +
            "          t1.`ID_`\n" +
            "        FROM\n" +
            "          (SELECT\n" +
            "            t.`ID_`,\n" +
            "            MAX(t.`VERSION_`)\n" +
            "          FROM\n" +
            "            `ACT_RE_PROCDEF` t\n" +
            "          GROUP BY t.`KEY_`) t1)\n" +
            "      )) t4\n" +
            "    ON t3.`ID_` = t4.`DEPLOYMENT_ID_` LIMIT #{page},#{limit}")
    List<DisDeployment> getDeployment(@Param("page") int page, @Param("limit") int limit);

    @Select("SELECT\n" +
            "  t3.`NAME_` AS `d_name`,\n" +
            "  t3.`KEY_` AS `d_key`,\n" +
            "  t3.`DEPLOY_TIME_` AS `deployment_time`,\n" +
            "  t4.`ID_`," +
            "  t3.`ENGINE_VERSION_`,\n" +
            "  t4.`NAME_` AS `p_name`,\n" +
            "  t4.`KEY_` AS `p_key`,\n" +
            "  t4.`VERSION_`,\n" +
            "  t4.`DEPLOYMENT_ID_`,\n" +
            "  t4.`RESOURCE_NAME_`,\n" +
            "  t4.`DGRM_RESOURCE_NAME_`  AS `diagram_resource_name`,\n" +
            "  t4.`SUSPENSION_STATE_`\n" +
            "FROM\n" +
            "  `ACT_RE_DEPLOYMENT` t3\n" +
            "  INNER JOIN\n" +
            "    (SELECT\n" +
            "      *\n" +
            "    FROM\n" +
            "      `ACT_RE_PROCDEF` t2\n" +
            "    WHERE t2.`ID_` IN (\n" +
            "        (SELECT\n" +
            "          t1.`ID_`\n" +
            "        FROM\n" +
            "          (SELECT\n" +
            "            t.`ID_`,\n" +
            "            MAX(t.`VERSION_`)\n" +
            "          FROM\n" +
            "            `ACT_RE_PROCDEF` t\n" +
            "          GROUP BY t.`KEY_`) t1)\n" +
            "      )) t4\n" +
            "    ON t3.`ID_` = t4.`DEPLOYMENT_ID_`")
    List<DisDeployment> getDeploymentAll();
}
